﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Domain.ViewModels.Settlement;
using WaterRevenueMS.Core.Service.Customer;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using System.Linq.Expressions;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using WaterRevenueMS.Componnent.Entities.Customer;
using WaterRevenueMS.Core.Service.BaseData;
using WaterRevenueMS.Domain.ViewModels.BaseData.MeterBook;

namespace WaterRevenueMS.WebUI.Controllers
{
    //计量结算
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class SettlementController : AdminController
    {
        [Import]
        public IMeterInfoService MeterInfoService { get; set; }

        [Import]
        private IMeterBookService MeterBookService { get; set; }

        [AdminLayout]
        public ActionResult Index()
        {
            var model = new SettlementModel();
            var meterBookItems = MeterBookService.MeterBooks.Where(t => t.IsDeleted == false)
                .Select(t => new MeterBookModel { 
                    id=t.Id,
                    Biao_CMC=t.Biao_CMC
                });
            foreach (var item in meterBookItems)
            {
                model.Search.MeterBookItems.Add(new SelectListItem {Text=item.Biao_CMC,Value=item.id.ToString() });
            }
            return View(model);
        }

        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = MeterInfoService.MeterInfos.Count(t => t.IsDeleted == false);

            var expr = BuildSearchCriteria();

            var filterResult = MeterInfoService.MeterInfos.Where(expr).Select(t => new SettlementModel
            {
                Id = t.Id,
                Hu_ZXM = t.Customer != null ? t.Customer.Hu_ZXM : "",
                Yong_HBH = t.Customer != null ? t.Customer.Yong_HBH : "",
                Yi_BBH = t.Yi_BBH,
                Yi_BDZ = t.Yi_BDZ,
                Du_SBQ = t.Du_SBQ,
                Chao_BDTBC = t.Chao_BDTBC,
                Du_SBC = t.Du_SBC,
                Shi_FJS=t.Shi_FJS,
                Jie_SDT=t.Jie_SDT,
                Yong_LBC=(t.Du_SBC-t.Du_SBQ),
                Ji_FBC=0

            }).OrderBy(t => t.Id).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.Hu_ZXM,
                            c.Yong_HBH,
                            c.Yi_BBH,
                            c.Yi_BDZ,
                            c.Du_SBQ.ToString(),
                            c.Du_SBC.ToString(),
                            c.Chao_BDTBC.ToString(),
                            c.Yong_LBC.ToString(),
                            c.Ji_FBC.ToString(),
                            c.Shi_FJS==0?"未结算":"已结算",
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }
        
        [AdminPermission(PermissionCustomMode.Ignore)]
        public string ChargeSettlement()
        {
            var expr = BuildSettlementCriteria();
            //计量结算
            MeterInfoService.ChargeSettlement(expr);
            return "{\"state\":\"1\"}";
            
        }

        private Expression<Func<MeterInfo, Boolean>> BuildSettlementCriteria()
        {
            DynamicLambda<MeterInfo> bulider = new DynamicLambda<MeterInfo>();
            Expression<Func<MeterInfo, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Biao_CSZ_No"]))
            {
                var data = Convert.ToInt32(Request["Biao_CSZ_No"]);
                Expression<Func<MeterInfo, Boolean>> tmp = t => t.Biao_CSZ_No == data;
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Yi_BBH"]))
            {
                var data = Request["Yi_BBH"].Trim();
                Expression<Func<MeterInfo, Boolean>> tmp = t => t.Yi_BBH.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Hu_ZXM"]))
            {
                var data = Request["Hu_ZXM"].Trim();
                Expression<Func<MeterInfo, Boolean>> tmp = t => t.Customer.Hu_ZXM.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Yong_HBH"]))
            {
                var data = Request["Yong_HBH"];
                Expression<Func<MeterInfo, Boolean>> tmp = t => t.Customer.Yong_HBH == data;
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            Expression<Func<MeterInfo, Boolean>> tmpSolid = t => t.Shi_FJS == 0;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }

        private Expression<Func<MeterInfo, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<MeterInfo> bulider = new DynamicLambda<MeterInfo>();
            Expression<Func<MeterInfo, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Biao_CSZ_No"]))
            {
                var data = Convert.ToInt32(Request["Biao_CSZ_No"]);
                Expression<Func<MeterInfo, Boolean>> tmp = t => t.Biao_CSZ_No==data;
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Yi_BBH"]))
            {
                var data = Request["Yi_BBH"].Trim();
                Expression<Func<MeterInfo, Boolean>> tmp = t => t.Yi_BBH.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Hu_ZXM"]))
            {
                var data = Request["Hu_ZXM"].Trim();
                Expression<Func<MeterInfo, Boolean>> tmp = t => t.Customer.Hu_ZXM.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            if (!string.IsNullOrEmpty(Request["Yong_HBH"]))
            {
                var data = Request["Yong_HBH"];
                Expression<Func<MeterInfo, Boolean>> tmp = t => t.Customer.Yong_HBH == data;
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            
            Expression<Func<MeterInfo, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }

    }
}
